package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.CardBo;
import com.groupseven.happycampus.common.pojo.Card;
import com.groupseven.happycampus.common.utils.BeanUtils;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.CardMapper;
import com.groupseven.happycampus.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BeLongsProject: happycampus
 * @BeLongsPackage: com.groupseven.happycampus.service.impl
 * @Author: WRH
 * @CreateTime: 2022-07-26 15:23
 * @Description: TODO
 * @Version: 1.0
 */
@Service("cardService")
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;

    @Override
    public Boolean insertCard(Map<String, Object> params) {
        Card card = new Card();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, card);

        Integer result = cardMapper.insertCard(card);

        return result != 0;
    }

    @Override
    public LayuiPageVo<Card> cardList(Map<String, Object> params) {


        int page = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));

        PageHelper.startPage(page,limit);

        List<Card> cardList = cardMapper.cardList(params);

        PageInfo<Card> cardPageInfo = new PageInfo<>(cardList);

        List<Card> list = cardPageInfo.getList();

        List<Card> cards = list.stream().map(o -> {
            if ("0".equals(o.getCardState())) {
                o.setCardState("未激活");
            }
            if ("1".equals(o.getCardState())) {
                o.setCardState("已激活");
            }
            if ("2".equals(o.getCardState())) {
                o.setCardState("挂失中");
            }
            return o;
        }).collect(Collectors.toList());

        return new LayuiPageVo<Card>(0,"卡号数据",cardPageInfo.getTotal(),cards);
    }

    @Override
    public Boolean updateCard(Map<String, Object> params) {
        Card card = new Card();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, card);

        Integer result = cardMapper.updateCard(card);

        return result != 0;
    }

    @Override
    public Boolean deleteCard(Map<String, Object> params) {
        Card card = new Card();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, card);

        Integer result = cardMapper.deleteCard(card);

        return result != 0;
    }

    @Override
    public boolean deleteCardsByIds(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(o -> Integer.parseInt(o)).collect(Collectors.toList());
        Integer result = cardMapper.deleteCardsByIds(list);
        return result >0;
    }

    @Override
    public void addCards(Card card) {
        cardMapper.addCards(card);
    }

    @Override
    public List<CardBo> cardExcelData(Card card) {
        List<Card> cards = cardMapper.selectCards(card);
        List<CardBo> cardBos = cards.stream()
                .map(o -> new CardBo(o.getCardId(), o.getUserName(), o.getUserId(), o.getCardState(), o.getCardBalance(), o.getCardFreezeTime()))
                .collect(Collectors.toList());

        return cardBos;
    }
}
